package com.supreme.aspectserver.aspect;

import com.supreme.aspectserver.annotation.OrderLockAnno;
import com.supreme.aspectserver.annotation.StockLockAnno;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Optional;

@Slf4j
@Order(1)
@Aspect
@Component
public class OrderAnnoAspect {

    @Pointcut(value = "@annotation(com.supreme.aspectserver.annotation.OrderLockAnno)")
    public void PointCut() {
    }

    @Around("PointCut()")
    public Object aroundProcess(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("[OrderAnnoAspect] Before");
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        OrderLockAnno orderLockAnno = method.getAnnotation(OrderLockAnno.class);
        Optional.ofNullable(orderLockAnno).ifPresent(orderLockAnno1 -> System.out.println(orderLockAnno1.value()));
        StockLockAnno stockLockAnno = method.getAnnotation(StockLockAnno.class);
        Optional.ofNullable(stockLockAnno).ifPresent(stockLockAnno1 -> System.out.println(stockLockAnno1.value()));
        Object proceed = joinPoint.proceed();
        log.info("[OrderAnnoAspect] After");
        return proceed;
    }
}
